package com.example.lijianwen.widgettest_401ljw;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private EditText et;
    private Button btngettext, btnsetimg;
    private ImageView iv;
    private ImageButton ib;

    private RadioGroup rg;
    private RadioButton rbnan;
    private RadioButton rbnv;
    private CheckBox cblan;
    private CheckBox cbzu;
    private CheckBox cbyou;
    private Button btngetgender;
    private Button bengetfun;
    private String strfunc="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        //获取六个控件
        tv=(TextView)findViewById(R.id.textView);
        et=(EditText) findViewById(R.id.editText);
        btngettext=(Button) findViewById(R.id.button);
        btnsetimg=(Button) findViewById(R.id.button2);
        iv=(ImageView) findViewById(R.id.imageView);
        ib=(ImageButton) findViewById(R.id.imageButton2);
        //设置监听器
        btngettext.setOnClickListener(this);
        btnsetimg.setOnClickListener(this);
        ib.setOnClickListener(this);

        rg=(RadioGroup) findViewById(R.id.rad);
        rbnan=(RadioButton) findViewById(R.id.radioButton7);
        rbnv=(RadioButton) findViewById(R.id.radioButton8);
        cblan=(CheckBox) findViewById(R.id.checkBox4);
        cbyou=(CheckBox) findViewById(R.id.checkBox5);
        cbzu=(CheckBox) findViewById(R.id.checkBox6);
        btngetgender=(Button) findViewById(R.id.button5);
        bengetfun=(Button) findViewById(R.id.button6);

        btngetgender.setOnClickListener(this);
        bengetfun.setOnClickListener(this);

        //radio button group状态改变
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId)
                {
                    case R.id.radioButton7:
                        tv.setText("男");
                        break;
                    case R.id.radioButton8:
                        tv.setText("女");
                        break;
                }
            }
        });

        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfunc+="游泳";
                else
                    strfunc = strfunc.replace("游泳","");
                tv.setText(strfunc);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked)
                    strfunc+="足球";
                else
                    strfunc = strfunc.replace("足球","");
                tv.setText(strfunc);
            }
        });
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfunc+="篮球";
                else
                    strfunc = strfunc.replace("篮球","");
                tv.setText(strfunc);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //设置文本
            case R.id.button:
                String str = et.getText().toString();
                tv.setText(str);
                break;
            //设置图片
            case R.id.button2:
                iv.setImageResource(R.drawable.b);
                break;
            //图片按钮
            case R.id.imageButton2:
                Intent intent = new Intent(MainActivity.this, newActivity.class);
                startActivity(intent);
                intent.putExtra("p1",1);
                intent.putExtra("p2","sb");
                break;
            case R.id.button5:
                if(rbnan.isChecked())
                    tv.setText("boy");
                if(rbnv.isChecked())
                    tv.setText("girl");
                break;
            case R.id.button6:
                String msg = "";
                if(cblan.isChecked())
                    msg+="baseketball ";
                if(cbzu.isChecked())
                    msg+="vollyball ";
                if(cbyou.isChecked())
                    msg+="swimming ";
                tv.setText(msg);
                break;
        }
    }


}
